package dbsync4j.core.concrete;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import dbsync4j.core.behavior.Procedure;
import dbsync4j.core.behavior.Schema;
import dbsync4j.core.behavior.Server;
import dbsync4j.core.behavior.Table;

/**
 * Abstrai os metadados de uma base de dados (database ou schema).
 * 
 * @author Rafael
 *
 */
public class ConcreteSchema extends AbstractDatabaseObject implements Schema {

	private Map<String, Table> tables = new HashMap<String, Table>();
	private Map<String, Procedure> procedures = new HashMap<String, Procedure>();
	private Server server;

	public ConcreteSchema(String name,
			String remarks,
			String schemaName,
			String catalogName){

		super(name, remarks, schemaName, catalogName);
	}

	/**
	 * Adiciona uma procedure na tabela.
	 * 
	 * @param procedure procedure
	 */
	public void addProcedure(Procedure procedure) {
		if(procedure == null) {
			
			throw new IllegalArgumentException("Procedure name is null!");
			
		} else if(procedure.getName() == null) {

			throw new IllegalArgumentException("Procedure name is null!");
			
		} else if(procedure.getName().equals("")) {

			throw new IllegalArgumentException("Procedure name is empty!");
			
		} else {

			procedures.put(procedure.getName(), procedure);
		}
		
	}
	
	/**
	 * Adiciona uma tabela no schema.
	 * 
	 * @param table tabela
	 */
	public void addTable(Table table) {
		if(table == null) {
			
			throw new IllegalArgumentException("Table name is null!");
			
		} else if(table.getName() == null) {

			throw new IllegalArgumentException("Table name is null!");
			
		} else if(table.getName().equals("")) {

			throw new IllegalArgumentException("Table name is empty!");
			
		} else {

			tables.put(table.getName(), table);
		}
	}

	@Override
	public Procedure getProcedure(String procedureName) {

		return procedures.get(procedureName);
	}

	@Override
	public Collection<Procedure> getProcedures() {
	
		return procedures.values();
	}

	@Override
	public Table getTable(String tableName) {

		return tables.get(tableName);
	}

	@Override
	public Collection<Table> getTables() {
	
		return tables.values();
	}
	
	public void setServer(Server server) {
		this.server = server;
	}

	@Override
	public Server getServer() {

		return this.server;
	}

}